// 获取文件类型
const getFileType = (fileName) => {
  let suffix = ""; // 后缀获取
  let result = ""; // 获取类型结果
  if (fileName) {
    const flieArr = fileName.split("."); // 根据.分割数组
    suffix = flieArr[flieArr.length - 1]; // 取最后一个
  }
  console.log("suffix", suffix);

  if (!suffix) return false; // fileName无后缀返回false
  suffix = suffix.toLocaleLowerCase(); // 将后缀所有字母改为小写方便操作
  // 匹配图片
  const imgList = ["png", "jpg", "jpeg", "bmp", "gif"]; // 图片格式
  // const imgList = ["bmp"]; // 图片格式
  result = imgList.find((item) => item === suffix);
  if (result) return ["image", result];
  // 匹配txt
  const txtList = ["txt"];
  result = txtList.find((item) => item === suffix);
  if (result) return ["txt", result];
  // 匹配excel
  const excelList = ["xls", "xlsx"];
  result = excelList.find((item) => item === suffix);
  if (result) return ["excel", result];
  // 匹配word
  const wordList = ["doc", "docx"];
  result = wordList.find((item) => item === suffix);
  if (result) return ["word", result];
  // 匹配pdf
  const pdfList = ["pdf"];
  result = pdfList.find((item) => item === suffix);
  if (result) return ["pdf", result];
  // 匹配ppt
  const pptList = ["ppt", "pptx"];
  result = pptList.find((item) => item === suffix);
  if (result) return ["ppt", result];
  // 匹配zip
  const zipList = ["rar", "zip", "7z"];
  result = zipList.find((item) => item === suffix);
  if (result) return ["zip", result];
  // 匹配视频
  const videoList = [
    "mp4",
    "m2v",
    "mkv",
    "rmvb",
    "wmv",
    "avi",
    "flv",
    "mov",
    "m4v",
  ];
  result = videoList.find((item) => item === suffix);
  if (result) return ["video", result];
  // 匹配音频
  const radioList = ["mp3", "wav", "wmv"];
  result = radioList.find((item) => item === suffix);
  if (result) return ["audio", result];
  // 匹配bt种子
  const torrentList = ["torrent"];
  result = torrentList.find((item) => item === suffix);
  if (result) return ["bt", result];
  // 匹配apk种子
  const apkList = ["apk"];
  result = apkList.find((item) => item === suffix);
  if (result) return ["apk", result];
  // 其他文件类型
  return ["other", suffix];
};
const category = [
  {
    type: ["video"],
    category: "1",
  },
  {
    type: ["image"],
    category: "2",
  },
  {
    type: ["excel", "word", "pdf", "ppt"],
    category: "3",
  },
  {
    type: ["audio"],
    category: "4",
  },
  {
    type: ["bt"],
    category: "5",
  },
  {
    type: ["other", "zip", "apk", "txt"],
    category: "6",
  },
];
// 获取相应文件类型的category分类
const getCategory = (fileName) => {
  // console.log("fileName", fileName);

  let type = getFileType(fileName)[0];
  // console.log("type", type);

  return category.filter((item) => item.type.includes(type))[0]?.category || '6';
};
module.exports = {
  getFileType,
  getCategory,
};
